home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Combine zero-offset with unit-offset arrays ?
- Date: 04 Mar 1996 15:31:59 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Mar4083159@qcd.lanl.gov>
- References: <4hes0h$52qi@info4.rus.uni-stuttgart.de>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: Markus Heller's message of 4 Mar 1996 13:42:41 GMT
-
- In article <4hes0h$52qi@info4.rus.uni-stuttgart.de>
- Markus Heller <Markus.Heller@studbox.uni-stuttgart.de> writes:
-
- <snip>
- MH: I want to combine my code (using zero-offset arrays) with
- MH: code using unit-offset arrays.
-
- Bad idea. C was not designed to handle non-zero offsets easily.
-
- MH: The FAQ - 2.16 mentions a method used "in old editions of
- MH: Numerical Recipes in C" that leads to undefined behavior.
- MH: Unfortunately there's no alternative mentioned.
-
- Because there is no _good_ alternative. You can declare float a[11]
- and use a[1] through a[10] (and waste the a[0]).
-
- MH: E.g.:
- MH:
- MH: void somefunction( float fa[], int n);
- MH:
- MH: int main(void)
- MH: {
- MH: float a[10]; /* zero offset: a[0] .. a[9] */
- MH: int n=10;
- MH:
- MH: somefunction( a-1, 10 ); /* method suggested by Numerical Recipes,
- MH: 2nd edition */
-
- For some reason, the authors of numerical recipes decided to
- transliterate their fortran programs into C. This is always a bad
- idea: just rewrite what you want in C.
-
- Numerical recipes in C reminds me of the German who could `bekam a
- cauliflower at the corner store'. Whatever it is, it is not C.
-
- MH: }
- MH:
- MH: void somefunction( float fa[], int n)
- MH: {
- MH: /* do something with a, assuming unit-offset: a[1]..a[10] */
- MH: }
- MH:
- MH:
- MH: Which alternatives are possible to the above mentioned method ?
- MH: (Is the method still leasding to undefined behavior ?)
-
- Yes. Decrementing beyond the beginning of an object leads to undefined
- behaviour.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-